package m3f.retrieval;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

import org.jblas.DoubleMatrix;

public class RandomReader {
	
	public String fileName = "";
	List<Long> lineIndex = new ArrayList<Long>();
	public RandomAccessFile raFile = null;
	
	public int features = 0;
	public int size = 0;
	
	RandomReader(String fileName) throws IOException{
		this.fileName = fileName;
		File file = new File(fileName);
		raFile = new RandomAccessFile(file,"r");
		long seek = 0;
		Long length = file.length();
		while(seek < length) { 
			System.out.println(lineIndex.size());
			lineIndex.add(seek);
			if (seek == 0){
				String s = raFile.readLine();
				features = s.split(" ").length;
			}else{
				raFile.readLine();
			}
			seek = raFile.getFilePointer();
		}
		size = lineIndex.size();
	}
	
	public void close() throws IOException{
		raFile.close();
	}
	
	public String getLine(int index){
		try {
			raFile.seek(lineIndex.get(index));
			return raFile.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public DoubleMatrix getVector(int index){
		String[] data = getLine(index).split(" ");
		DoubleMatrix vector = DoubleMatrix.zeros(1, features);
		for (int i = 0; i < features; i++){
			vector.put(1, i - 1, Double.parseDouble(data[i]));
		}
		return vector;
	}
	
	public static void main(String[] args) throws IOException {
		RandomReader rr = new RandomReader("/Users/macbook/Workspace/testFile.txt");
		System.out.println(rr.getLine(0));
		
	}
	
	
}
